/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.accident.controller;

import com.koron.accident.domain.WorkTime;
import com.koron.accident.service.api.WorkTimeService;
import com.koron.accident.util.BusinessCode;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

@Api(tags = "workTime",description = "工时管理相关接口类")
@RestController
@RequestMapping("/workTime")
public class WorkTimeController {

    @Autowired
    private WorkTimeService workTimeService;

    @ApiOperation(value = "保存工时记录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id",  dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "departmentCode", value = "单位code", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "departmentName", value = "单位名称", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "years", value = "工时年月", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "days", value = "本月总天数", required = true, dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "dayWorkTimes", value = "人天工时", required = true, dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "lossWorkTimes", value = "损耗工时", required = true, dataType = "float",paramType = "query"),
            @ApiImplicitParam(name = "staffNumber", value = "人数", required = true, dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "totalWorkTimes", value = "工时小计", required = true, dataType = "float",paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注",  dataType = "String",paramType = "query")
    })
    @RequestMapping(value = "/save.htm",method = RequestMethod.POST)
    public MessageBean<Integer> save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@ApiIgnore WorkTime bean){
        if (null==user){
            MessageBean<Integer> messageBean=new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LONGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return messageBean;
        }
        bean.setUpdateTime(new Date());
        bean.setUpdateUser(user.getCode());
        bean.setUpdateUserName(user.getName());
        return workTimeService.save(bean);
    }

    @ApiOperation("删除工时记录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "事故id",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/delete.htm",method = RequestMethod.POST)
    public MessageBean<Integer> delete(@RequestParam final String id){
        return workTimeService.delete(id);
    }

    @ApiOperation("查询单个工时记录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "工时记录id",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/details.htm",method = RequestMethod.POST)
    public MessageBean<WorkTime> details(@RequestParam final String id){
        return workTimeService.queryOne(id);
    }

    @ApiOperation("工时记录列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode",value = "水司code",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "departmentCode",value = "事发单位",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "startYears",value = "工时年月，开始，格式为：201901",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "endYears",value = "工时年月，结束，格式为：201902",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,dataType = "int",paramType = "query"),
    })
    @RequestMapping(value = "/list.htm",method = RequestMethod.POST)
    public MessageBean<DataBean<WorkTime>> list(@RequestParam(required = false) final String corpCode,@RequestParam(required = false) final String departmentCode, @RequestParam(required = false) final String startYears,
                                                @RequestParam(required = false) final String endYears, @RequestParam Integer page, @RequestParam Integer pageSize){
        Integer start=null;
        if (page==null||page<=0){
            page=1;
            if (pageSize==null||pageSize<=0){
                pageSize=15;
            }
        }
        start=(page<=1)?0:(page-1)*pageSize;
        MessageBean<DataBean<WorkTime>> msg= workTimeService.list(corpCode,departmentCode,startYears,endYears,start,pageSize);
        if (null!=msg.getData())
            msg.getData().setPageNo(page);
        return msg;
    }
}
